package com.cow.support.util;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;


public class MapToBeanUtil {

    @SuppressWarnings("rawtypes")  
    public Object convertMap(Class type, Map map)  
            throws IntrospectionException, IllegalAccessException,  
            InstantiationException, InvocationTargetException {  
        BeanInfo beanInfo = Introspector.getBeanInfo(type); 
        Object obj = type.newInstance();
  
        PropertyDescriptor[] propertyDescriptors =  beanInfo.getPropertyDescriptors();  
        for (int i = 0; i< propertyDescriptors.length; i++) {  
            PropertyDescriptor descriptor = propertyDescriptors[i];  
            String propertyName = descriptor.getName();  
  
            if (map.containsKey(propertyName)) {  
                Object value = map.get(propertyName);  
  
                Object[] args = new Object[1];  
                args[0] = value;  
  
                descriptor.getWriteMethod().invoke(obj, args);  
            }  
        }  
        return obj;  
    }  
    

    @SuppressWarnings("rawtypes")  
    public static void copyMapToBean(Map map,Object obj){  
    	try{
    		BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass()); // ��ȡ������  
    		  

            PropertyDescriptor[] propertyDescriptors =  beanInfo.getPropertyDescriptors();  
            for (int i = 0; i< propertyDescriptors.length; i++) {  
                PropertyDescriptor descriptor = propertyDescriptors[i];  
                String propertyName = descriptor.getName(); 
                String xclass = descriptor.getPropertyType().getName();
      
                if (map.containsKey(propertyName)) {  
                	Method setMethod = descriptor.getWriteMethod();
                	if(map.get(propertyName) == null){
                		setMethod.invoke(obj, new Object[]{null});
                		continue;
                	}
                	                	

                    String value = map.get(propertyName).toString();    
                                        
     
    				if (xclass.equals("java.lang.String")) {
    					setMethod.invoke(obj, value);
    				} else if (xclass.equals("java.util.Date")) {
    					Date getFetchDate = null;
    					if(value.length()==10){
    						getFetchDate = DateUtil.parse(value);
    					}else if(value.length()==19){
    						getFetchDate = DateUtil.parseDateTime(value);
    					}
    					
    					setMethod.invoke(obj, getFetchDate);
    				} else if (xclass.equals("java.lang.Boolean")) {
    					setMethod.invoke(obj, Boolean.parseBoolean(value));
    				} else if (xclass.equals("java.lang.Integer")) {
    					setMethod.invoke(obj, new Integer(value));
    				} else if (xclass.equals("java.lang.Long")) {
    					setMethod.invoke(obj, new Long(value));
    				} else if (xclass.equals("java.lang.Float")) {
    					setMethod.invoke(obj, new Float(value));
    				} else if (xclass.equals("java.lang.Double") || xclass.equals("double")) {
    					setMethod.invoke(obj, new Double(value));
    				} else if (xclass.equals("java.math.BigDecimal")) {
    					setMethod.invoke(obj, new BigDecimal(value));
    				} else {
    					try{
    						setMethod.invoke(obj, value);
    					}catch(Exception e){
    						
    					}
    					
    				}
                }  
            } 
    	}catch(Exception e){
    		e.printStackTrace();
    	}         
    } 
}
